﻿using Engine;
using Game;
using Game.NetWork;
using Game.NetWork.Packages;
using GameEntitySystem;
using MFTrain;
using TemplatesDatabase;

namespace Test1
{
    public class FCComponentPC : Component, IUpdateable
    {
        public SubsystemTerrain m_systemTerrain;

        public SubsystemTime m_subsystemTime;

        public ComponentPlayer m_componentPlayer;

        public ComponentCreature m_componentCreature;

        public ComponentTest1 m_componentTest1;

        public SubsystemFluidBlockBehavior m_fluidBlockBehavior;

        public ComponentMFTrain componenttrain;

        public ComponentBody m_componentBody;

        public bool isonTrain;

        public float g;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public float Gravity => 10f - g;

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_systemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_fluidBlockBehavior = base.Project.FindSubsystem<SubsystemFluidBlockBehavior>(throwOnError: true);
            m_componentTest1 = base.Entity.FindComponent<ComponentTest1>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            m_componentBody = base.Entity.FindComponent<ComponentBody>(throwOnError: true);
            isonTrain = valuesDictionary.GetValue("isonTrain", defaultValue: false);
            base.Load(valuesDictionary, idToEntityMap);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            base.Save(valuesDictionary, entityToIdMap);
            valuesDictionary.SetValue("isonTrain", isonTrain);
        }

        public void Update(float dt)
        {
            if (m_componentBody.ParentBody != null && !isonTrain)
            {
                m_componentPlayer.ComponentHealth.FallResilience = 9999f;
                isonTrain = true;
            }
            else if (m_componentBody.ParentBody == null && isonTrain)
            {
                m_componentPlayer.ComponentHealth.FallResilience = 12f;
                isonTrain = false;
            }

            if (m_componentPlayer == null)
            {
                return;
            }

            float num = 2f;
            if (m_componentPlayer.ComponentVitalStats.Food <= 0.3f)
            {
                num += 1f;
            }

            if (m_componentPlayer.ComponentVitalStats.Sleep <= 0.3f)
            {
                num += 1f;
            }

            if (m_componentPlayer.ComponentHealth.Health <= 0.5f)
            {
                if (m_componentTest1 != null && m_componentTest1.m_sen >= 0f && CommonLib.WorkType != WorkType.Client && Time.PeriodicEvent(8.0, 0.0))
                {
                    m_componentTest1.m_sen -= num;
                    CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncStat));
                }
            }
            else if (m_componentPlayer.ComponentHealth.Health >= 0.95f && m_componentTest1.m_sen < 100f && m_componentTest1.Areaname != "失落城市" && CommonLib.WorkType != WorkType.Client && Time.PeriodicEvent(2.0, 0.0))
            {
                m_componentTest1.m_sen += 1f;
                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncStat));
            }

            if (m_componentTest1.m_sen <= 0f && m_subsystemTime.PeriodicGameTimeEvent(3.0, 0.0))
            {
                m_componentPlayer.ComponentGui.DisplaySmallMessage("sen值归0会每3秒受到生命值15%的固定伤害！", Color.White, blinking: true, playNotificationSound: true);
                ComponentHealth componentHealth = m_componentPlayer.Entity.FindComponent<ComponentHealth>();
                if (componentHealth.Health > 0.15f)
                {
                    componentHealth.Health -= 0.15f;
                }
                else
                {
                    componentHealth.Health = 0f;
                }

                CommonLib.Net.QueuePackage(new ComponentHealthPackage(componentHealth));
            }

            if (!(m_componentTest1.m_mp < 1f) || !(m_componentTest1.m_mp > 0f) || !(m_componentTest1.Areaname != "血泪之池") || !(MathUtils.Floor(m_componentPlayer.PlayerData.Level) > 7f) || CommonLib.WorkType == WorkType.Client || !Time.PeriodicEvent(8.0, 0.0))
            {
                return;
            }

            if (m_componentTest1.FCMagicLevel == 1)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 1f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }

            if (m_componentTest1.FCMagicLevel == 2)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 5f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }

            if (m_componentTest1.FCMagicLevel == 3)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 8f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }

            if (m_componentTest1.FCMagicLevel == 4)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 10f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }

            if (m_componentTest1.FCMagicLevel == 5)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 50f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }

            if (m_componentTest1.FCMagicLevel == 6)
            {
                m_componentTest1.m_mp += 1f / m_componentTest1.MaxMagicPower * 100f;
                if (m_componentTest1.m_mp > 1f)
                {
                    m_componentTest1.m_mp = 1f;
                }

                CommonLib.Net.QueuePackage(new ComponentTest1Package(m_componentTest1, ComponentTest1Package.EventType.SyncMagic, 1));
            }
        }
    }
}